package edu.sctu.zhangwang.month201506.day20150613;


import java.io.*;
import java.util.Scanner;

/**
 * Created by Administrator on 2015/6/13.
 */
public class InputStream {
    public static void main(String[] args) throws IOException {
        File f = new File("E://my_file/zhangwang.txt");
        FileOutputStream fop = new FileOutputStream(f);
        //构建FILEOutputStream对象，文件不存在会自动新建
        OutputStreamWriter writer = new OutputStreamWriter(fop,"UTF-8");
        //构建OutputStreamWriter对象，参数可以指定编码，默认为操作系统默认编码，windows上是gdk
        Scanner input  = new Scanner(System.in);
        System.out.println("请输入中文内容：");
        String a = input.next();
        writer.append(a);
        //写到缓冲区
        writer.append("\r\n");
        //换行
        writer.append("English");
        //刷新缓冲，写入到文件，如果下面已经没有写入的内容了，直接close也会写入
        writer.close();
        //关闭写入流，同时会把缓冲区内容写入文件，所以上面的注释掉
        fop.close();
        //关闭输入释放系统资源
        FileInputStream fip = new FileInputStream(f);
        //构建FileInputStream对象
        InputStreamReader reader = new InputStreamReader(fip,"UTF-8");
        //构建InputStreamReader对象，编码与写入相同
        StringBuffer sb = new StringBuffer();
        while (reader.ready()){
            sb.append((char) reader.read());
            //转成char加到StringBuffer对象中
        }
        System.out.println("你输入的内容是："+sb.toString());
        reader.close();
        //关闭读取流

        fip.close();
    }
}
